import { Component, ElementRef, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import { RouterModule, Router, ActivatedRoute } from '@angular/router';

import { Service } from '../../../server/server';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
declare var $: any;

@Component({
  selector: 'app-central-add',
  templateUrl: './central-add.component.html'
})
export class CentralAddComponent implements OnInit {

  constructor(private http: HttpClient, private router: Router, private server: Service, private cudTableService: CudTableService) { }

  createUserId: any;
  //datatype
  centraladd: centraladdtype = { name: '', description: '',id:'',createdate:'',operateType:"0" };
  ngOnInit() {
    this.createUserId = this.server.getuser();
    //console.log(this.createUserId)
  }
  //清空
  @ViewChild('newlyAddedForm') formElement: HTMLFormElement

  reset() {
    this.formElement.reset();
    $("#add").modal("hide");
  }

  @Output()
  addOutput: EventEmitter<any> = new EventEmitter();

  public publicBtn:boolean = true;
  submitAdd() {
    this.centraladd.operateType = "0";
    let url = this.server.baseUrl + 'Service/Center/insertAndUpdateCenter';
    let data: centraladdtype = this.centraladd;
    //console.log(data)
    this.cudTableService.postRequest(url, data).then((res: any) => {
      this.addOutput.emit(res.result_code);
      //console.log(res.result_code)
      if (res.result_code == '0000') {
          this.reset();
          //prompt
          this.server.prompt = { prompt: true, success: true, fail: false, promptStatement: res.message };
          this.server.promptclose;      
        //update

      } else {
        this.server.prompt = { prompt: true, success: false, fail: true, promptStatement: res.message };
        this.server.promptclose;
      }
    }).catch()
  }
}
export class centraladdtype {
  name: string;
  description: string;
  createdate:'';
  id:'';
  operateType:string;
}
